const app = getApp();
var convert2ChartData = require('../../../utils/dataConvert.js')
var bridgeConfig = require('../../../utils/bridge.js')

Page({
  data: {
    id: null,
    userId: '',
    userName: '',
    editable: false,
    myBrdAttrMap: null,
    othBrdAttrMap: null,
    bridgeData: null,
    bridgeInfo: bridgeConfig.bridgeInfo,
    bridgeIndex: bridgeConfig.bridgeIndex,
    bridgeEfficiencyIndex: bridgeConfig.bridgeEfficiencyIndex,
    extendFields:null
  },
  // 跳转到编辑页面
  editBridgeInfo: function(e) {
    let bridgeData = JSON.stringify(this.data.bridgeData);
    let id = this.data.id;
    let userId = this.data.userId;
    let userName = this.data.userName;
    wx.navigateTo({
      url: '../edit/edit?bridgeData=' + bridgeData + '&id=' + id + '&userId=' + userId + '&userName=' + userName
    })
  },

  // 拿到我的桥梁
  getMyBridges: function() {
    let that = this;
    let myBridges = wx.getStorageSync('myBridges');
    that.setData({
      myBridges: myBridges,
      myBrdAttrMap: convert2ChartData.convert2ChartData(myBridges)
    });
  },

  // 拿到其他桥梁
  getOther: function() {
    var that = this;
    let otherBridges = wx.getStorageSync('otherBridges');
    that.setData({
      otherBridges: otherBridges,
      othBrdAttrMap: convert2ChartData.convert2ChartData(otherBridges)
    });
  },
  viewBridgeIndex: function(e) {
    let key = e.currentTarget.dataset.itemname + '-' + this.data.bridgeData.structureTypeName;
    let indexInfo = {
      structureName: this.data.bridgeData.structureTypeName,  // 桥梁结构类型
      spanExpression: this.data.bridgeData.spanExpression,    // 跨径表达式
      averageSpan: this.data.bridgeData.avgAcross,            // 平均跨径
      bridgeWidth: this.data.bridgeData.width,                // 桥宽
      title: e.currentTarget.dataset.title,                   // 指标图页面标题
      description: e.currentTarget.dataset.description,       // 指标图页面标题描述
      itemTitle: e.currentTarget.dataset.itemtitle,           // Y轴标题（含单位）
      itemName: e.currentTarget.dataset.itemname,             // 指标名字
      itemUnit: e.currentTarget.dataset.unit,                 // 该指标Y坐标单位
      itemValue: e.currentTarget.dataset.itemvalue,          // 该指标Y坐标值
      myBridgeIndexData: this.data.myBrdAttrMap ? this.data.myBrdAttrMap.get(key) : [],
      otherBridgeIndexData: this.data.othBrdAttrMap ? this.data.othBrdAttrMap.get(key) : []
    };

    wx.navigateTo({
      url: '../show_chart/show_chart?indexInfo=' + JSON.stringify(indexInfo)
    });
  },
  onLoad: function(option) {
    var structureId = '';
    var token = app.globalData.openId;

    if (option && option.bridgeInfo) {
      let currentBridgeInfo = JSON.parse(option.bridgeInfo);
      this.setData({
        bridgeData: currentBridgeInfo,
        id: currentBridgeInfo.id,
        userId: currentBridgeInfo.userId,
        userName: currentBridgeInfo.userName,
        editable: wx.getStorageSync('openId') == currentBridgeInfo.userId,
        extendFields: currentBridgeInfo.extendFields
      });

      this.initBridgeData(currentBridgeInfo);
    }

    this.getMyBridges();
    this.getOther();
  },
  initBridgeData: function(bridgeData) {
    for (let item of this.data.bridgeInfo) {
      item["val"] = bridgeData[item.name];
      item["indexType"] = 'bridgeInfo';
      item["displayTitle"] = item.unit ? item.title + '(' + item.unit + ')' : item.title;
    }

    this.setData({
      bridgeInfo: this.data.bridgeInfo
    });

    for (let item of this.data.bridgeIndex) {
      item["val"] = bridgeData[item.name];
      item["indexType"] = 'bridgeIndex';
      item["displayTitle"] = item.unit ? item.title + '(' + item.unit + ')' : item.title;
    }

    this.setData({
      bridgeIndex: this.data.bridgeIndex
    });

    for (let item of this.data.bridgeEfficiencyIndex) {
      item["val"] = bridgeData[item.name];
      item["indexType"] = 'bridgeEfficiencyIndex';
      item["displayTitle"] = item.unit ? item.title + '(' + item.unit + ')' : item.title;
    }

    this.setData({
      bridgeEfficiencyIndex: this.data.bridgeEfficiencyIndex
    });
  }
})